# -*- coding: utf-8 -*-

from typing import List
from enum import Enum


class LdapConfig:
    user_account: str
    user_password: str

    def __init__(self, user_account: str, user_password: str):
        self.user_account = user_account
        self.user_password = user_password


class GitProject:
    name: str = ""
    project_path: str = ""
    gitee_path: str = ""
    ops_code: str = ""
    jenkins_job: str = ""
    ops_only_ng: bool = False
    ops_only_ding_talk: bool = False
    need_stash: bool = False
    has_conflict: bool = False
    branches_no_diff: bool = False
    merged_down: bool = False
    merged_up: bool = False
    pr_created: bool = False
    ops_created: bool = False
    jenkins_deployed: bool = False
    tag_operated: bool = False
    fail_msg: str = ""


class GiteeConfig:
    """
    config descriptions, see: https://gitee.com/api/v5/swagger#/postV5ReposOwnerRepoPulls
    """
    access_token: str
    owner: str
    pull_request_title: str
    do_pull: bool


class OpsConfig:
    """
    ops link: https://ops.maycur.com/
    """
    ldap_config: LdapConfig
    access_token: str = ""
    request_description: str = ""
    do_make_request: bool = False
    # 如果开启，需要提交旗舰版部署申请
    ops_ng: bool = True
    # 如果开启，需要提交钉版部署申请
    ops_dingtalk: bool = True

    def __init__(self, ldap_config):
        self.ldap_config = ldap_config

    def set_access_token(self, token: str):
        self.access_token = token


class JenkinsConfig:
    ldap_config: LdapConfig
    base_url: str = "http://s1.maycur.cc:4000/"
    do_deploy: bool = False
    force_deploy: bool = False

    def __init__(self, ldap_config):
        self.ldap_config = ldap_config


class JenkinsJobAdapter:
    name: str = ""
    prefix: str = ""
    suffix: str = ""

    def __init__(self, name: str):
        self.name = name


class GitBranchPair:
    pair_name: str = ""
    down_branch: str = ""
    up_branch: str = ""
    # gitee_base_branch is the target branch of a PullRequest
    gitee_base_branch: str = ""
    # ops_branch is used to make ops deploy request
    ops_branch: str = ""
    # jenkins_job_adapters is used to handle Jenkins job name, such as prefix and suffix
    jenkins_job_adapters: List[JenkinsJobAdapter] = None
    projects: List[GitProject] = None

    def __init__(self):
        self.jenkins_job_adapters = []
        self.projects = []

    def add_jenkins_job_adapter(self, job_adapter: JenkinsJobAdapter):
        self.jenkins_job_adapters.append(job_adapter)

    def add_project(self, project: GitProject):
        self.projects.append(project)


class TagOperateType(Enum):
    # create
    C = "创建"
    # delete
    D = "删除"

    @staticmethod
    def _type_dict():
        return {
            'create': TagOperateType.C,
            'delete': TagOperateType.D
        }

    @staticmethod
    def from_str(operate_type: str):
        if str is None:
            return None
        the_type = operate_type.strip()
        if the_type not in TagOperateType._type_dict():
            return None

        return TagOperateType._type_dict()[operate_type]


class GitTagConfig:
    name: str = ""
    comment: str = ""
    operate_type: TagOperateType
    do_operate: bool = False

    def __init__(self, name: str, operate_type: str):
        self.name = name
        self.operate_type = TagOperateType.from_str(operate_type)
        if self.operate_type is None:
            print(f'tag operation type: {operate_type} is not supported')


class GitConfig:
    """
    Git配置
    """
    base_path: str = ""
    branch_pairs: List[GitBranchPair] = []
    merge_description: str = ""
    do_merge: bool = True
    merge_down: bool = False
    merge_up: bool = False
    gitee_config: GiteeConfig = None
    ops_config: OpsConfig = None
    jenkins_config: JenkinsConfig = None
    tag_config: GitTagConfig = None

    def __init__(self, base_path, merge_description, do_merge, merge_down, merge_up):
        self.base_path = base_path
        self.merge_description = merge_description
        self.do_merge = do_merge
        self.merge_down = merge_down
        self.merge_up = merge_up

    def add_branch_pair(self, branch_pair):
        self.branch_pairs.append(branch_pair)

    def set_gitee_config(self, gitee_config: GiteeConfig):
        self.gitee_config = gitee_config

    def set_ops_config(self, ops_config: OpsConfig):
        self.ops_config = ops_config
        
    def set_jenkins_config(self, jenkins_config: JenkinsConfig):
        self.jenkins_config = jenkins_config

    def set_tag_config(self, tag_config: GitTagConfig):
        self.tag_config = tag_config
